package com.qingluan.dd;

import org.json.JSONException;
import org.json.JSONObject;

import com.qingluan.DataFormat.MissionTag;
import com.qingluan.DataFormat.MissionTag.receiveMission;
import com.qingluan.Database.MissionDataBase;
import com.qingluan.Database.TempDataBase;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class IfRecieveDialog extends Activity {
	//context arguments area
	Context context ;
	private static String tag = IfRecieveDialog.class.getName();
	private String uid ;
	//token area
	private String token_id;
	private String token_key;
		
	//info area
	private String mission_id;
	private String up_email;
	private String [] mission_info;
	//widget area 
	private Button commit_bt ;
	private TextView mission_detial_view;
	
	//context area
	private Context context_this_activity;
	private ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_if_recieve_dialog);
		context_this_activity = getApplicationContext();
		
		this.context = getApplicationContext();
		commit_bt = (Button)findViewById(R.id.sure_this_mission);
		Intent re_intent = getIntent();
		
		mission_detial_view = (TextView)findViewById(R.id.mission_detail);
		
		uid = re_intent.getStringExtra("uid");
		mission_id = re_intent.getStringExtra("mission_id");
		up_email = re_intent.getStringExtra("up_email");
		token_id = getIntent().getExtras().getString("token_id");
		token_key = getIntent().getExtras().getString("token_key");
		Log.d(tag, "token id : "+ token_id+" key:"+token_key);
		
		String mission_describe = new TempDataBase(context_this_activity).search(mission_id, TempDataBase.MISS_OBJ_ID, TempDataBase.DESCRIBE);
		mission_detial_view.setText(mission_describe);
		
		mission_info =new String[] {mission_id , up_email};
		Log.d("CACHE", "T T mission id :"+mission_id);
	}

	
	public void receiveView (View view){
		progressDialog = ProgressDialog.show(IfRecieveDialog.this,"select mission", "commiting..", true,false);
		MissionTag.ReceiveMission(context,mission_info, new receiveMission() {
			
			@Override
			public void sureMission(String info) {
				// TODO Auto-generated method stub
				/*
				 * for data save ...
				 */
				JSONObject jsonInfo = null;
				//IfRecieveDialog.this.setProgressBarIndeterminateVisibility(true);
				try {
					jsonInfo = new JSONObject(info);
					if (jsonInfo.getString("response").equals("ok")){
						String mission_id = MissionTag.ParaJsonForSave(context, jsonInfo);
						Log.d("CACHE","json info"+jsonInfo.toString());
						ContentValues oneMission = new TempDataBase(context).getMission(mission_id);
						Log.d("IfReceiveDialog","place name in DB "+oneMission.get(TempDataBase.PLACE_NAME));
						if (! new MissionDataBase(context).IfExist(mission_id)){
							new MissionDataBase(context).insertMission(oneMission);
						}
				

						Intent intent  = new Intent(IfRecieveDialog.this,MissionListFullscreenActivity.class);
						intent.putExtra("receive", "yes");
						intent.putExtra("mission_id", mission_id);
						intent.putExtra("uid", uid);
						intent.putExtra("token_id", token_id);
						intent.putExtra("token_key", token_key);
						
						progressDialog.dismiss();
						IfRecieveDialog.this.startActivity(intent);
						IfRecieveDialog.this.finish();
					}else{
						Log.d("CACHE",info);
						Toast.makeText(context, "获取失败", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

			@Override
			public void errorHandler(String info, String error) {
				// TODO Auto-generated method stub
				commit_bt.setVisibility(View.VISIBLE);
				Toast.makeText(context, info+"\n"+error, Toast.LENGTH_SHORT).show();
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.if_recieve_dialog, menu);
		return true;
	}

}
